package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.PurchaseDetailsMapper;
import com.spark.incoicings.dao.PurchaseReturnDetailsMapper;
import com.spark.incoicings.dao.PurchaseReturnOrderMapper;
import com.spark.incoicings.service.PurchaseReturnDetailsService;
import com.spark.invoicings.entity.PurchaseDetail;
import com.spark.invoicings.entity.PurchaseReturnDetails;
import com.spark.invoicings.entity.PurchaseReturnOrder;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseReturnDetailsServiceImpl extends ServiceImpl<PurchaseReturnDetailsMapper, PurchaseReturnDetails> implements PurchaseReturnDetailsService {

    @Resource
    private PurchaseReturnDetailsMapper purchaseReturnDetailsMapper;

    @Resource
    private PurchaseDetailsMapper purchaseDetailsMapper;

    @Resource
    private PurchaseReturnOrderMapper purchaseReturnOrderMapper;

    /**
     *  更新采购退货明细
     * @param returnDetails
     * @return
     */
    @Override
    public Result updatePurchaseReturnDetails(PurchaseReturnDetails returnDetails) {
        // 计算应退款金额
        returnDetails.setRefundAmount(returnDetails.getReturnQuantity() * returnDetails.getReturnUnitPrice());
        // 应退还总金额
        double totalRefundAmount = 0;
        // 已退还总数
        int totalReturnedQty = 0;
        // 更新采购退货单数量及总金额
        PurchaseReturnOrder purchaseReturnOrder = purchaseReturnOrderMapper.selectPurchaseReturnOrderById(returnDetails.getPurchaseReturnOrderId());
        for (PurchaseReturnDetails purchaseReturnDetails : purchaseReturnOrder.getPurchaseReturnDetailsList()) {
            totalRefundAmount += purchaseReturnDetails.getRefundAmount();
            totalReturnedQty += purchaseReturnDetails.getReturnQuantity();
        }
        purchaseReturnOrder.setTotalRefundAmount(totalRefundAmount);
        purchaseReturnOrder.setTotalReturnedQty(totalReturnedQty);
        purchaseReturnOrderMapper.updateById(purchaseReturnOrder);
        purchaseReturnDetailsMapper.updateById(returnDetails);
        return Result.success(200, "success", returnDetails.getPurchaseReturnDetailId());
    }


    /**
     * 保存采购退货明细
     * @param returnDetails
     * @return
     */
    @Override
    public Result addPurchaseReturnDetails(PurchaseReturnDetails returnDetails) {
        // 计算应退款金额
        returnDetails.setRefundAmount(returnDetails.getReturnQuantity() * returnDetails.getReturnUnitPrice());
        // 应退还总金额
        double totalRefundAmount = 0;
        // 已退还总数
        int totalReturnedQty = 0;
        // 更新采购退货单数量及总金额
        PurchaseReturnOrder purchaseReturnOrder = purchaseReturnOrderMapper.selectPurchaseReturnOrderById(returnDetails.getPurchaseReturnOrderId());
        for (PurchaseReturnDetails purchaseReturnDetails : purchaseReturnOrder.getPurchaseReturnDetailsList()) {
            totalRefundAmount += purchaseReturnDetails.getRefundAmount();
            totalReturnedQty += purchaseReturnDetails.getReturnQuantity();
        }
        purchaseReturnOrder.setTotalRefundAmount(totalRefundAmount);
        purchaseReturnOrder.setTotalReturnedQty(totalReturnedQty);
        purchaseReturnOrderMapper.updateById(purchaseReturnOrder);
        purchaseReturnDetailsMapper.insert(returnDetails);
        return Result.success(200, "success", returnDetails.getPurchaseReturnDetailId());
    }

    /**
     * 查询所有采购退货明细
     * @param map
     * @return
     */
    @Override
    public List<PurchaseReturnDetails> selectPurchaseReturnDetailsAll(Map<String, Object> map) {
        List<PurchaseReturnDetails> purchaseReturnDetails = purchaseReturnDetailsMapper.selectPurchaseReturnDetailsAll(map);
        purchaseReturnDetails.forEach(returnDetails -> {
            PurchaseDetail purchaseDetail = purchaseDetailsMapper.selectPurchaseDetailsById(returnDetails.getPurchaseOrderDetail());
            returnDetails.setPurchaseDetail(purchaseDetail);
        });
        return purchaseReturnDetails;
    }

    /**
     * 根据id获取采购退货明细
     * @param id
     * @return
     */
    @Override
    public PurchaseReturnDetails selectPurchaseReturnDetailsById(Integer id) {
        PurchaseReturnDetails purchaseReturnDetails = purchaseReturnDetailsMapper.selectPurchaseReturnDetailsById(id);
        PurchaseDetail purchaseDetail = purchaseDetailsMapper.selectPurchaseDetailsById(purchaseReturnDetails.getPurchaseOrderDetail());
        purchaseReturnDetails.setPurchaseDetail(purchaseDetail);
        return purchaseReturnDetails;
    }
}
